﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace RichLabelSpace
{
    namespace Warp
    {
        public class RichLabelWarp
        {
            private string m_naturl_str = "";
            private string m_temp_richstring = "";
            private static List<RichString> m_string_list = new List<RichString>();
            public List<RichString> RichStringList
            {
                get { return m_string_list; }
            }

            private Stack<RichString> m_rich_string_stack = new Stack<RichString>();
            internal Stack<RichString> RichStringStack { get { return m_rich_string_stack; } }


            // 外部调用
            public string Text
            {
                get { return m_naturl_str; }
                set
                {
                    this.m_naturl_str = value;
                    var list = Disintegrate(this.m_naturl_str);
                    foreach (var item in list)
                    {
                        Debug.Log(item);
                    }
                    foreach (var item in list)
                    {
                        m_rich_string_stack.Clear();
                        SetText(item);
                    }

                }
            }

            internal string TempRichString
            {
                get { return this.m_temp_richstring; }
            }

            // 解析出完整的html元素列表
            internal List<string> Disintegrate(string val)
            {
                var list = new List<string>();
                while (val.Contains("<") && val.Contains(">"))
                {
                    val = val.TrimStart(' ').TrimEnd(' ');
                    var start_index = val.IndexOf("<");
                    var end_index = start_index;
                    var start_type = RichStringTools.CheckType(val);
                    if (start_type == RichStringType.SimpleLab)
                    {
                        if (start_index > 0)
                        {
                            val = val.Remove(0, start_index);
                        }
                        else if (start_index == 0)
                        {
                            var next_index = val.IndexOf("<", 1);
                            val = next_index >= 0 ? val = val.Remove(0, next_index) : "";
                        }
                        else
                        { 
                            val = ""; 
                        }
                        continue;
                    }

                    var _item_str = RichStringTools.SpliteWholeRichStringByType(start_type, val);
                    if (!string.IsNullOrEmpty(_item_str) && val.Substring(0, _item_str.Length) == _item_str)
                    {
                        val = val.Remove(0, _item_str.Length);
                        list.Add(_item_str);
                    }
                    else
                    {
                        // 数据配置有误
                        Debug.Log("error richlabel  text: " + val);
                        val = RichStringTools.MakeLabelElement(val);
                    }
                }
                return list;
            }

            internal void SetText(string new_text)
            {
                if (new_text.Length > 0)
                {
                    var _comp = new RichString(this);
                    _comp.Disintegrate(new_text);
                }
            }

            internal void AddRichString(RichString rs)
            {
                m_string_list.Add(rs);
            }
        }
    }
}
